package com.cema.manage.project.android.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.antirisk20221128.models.GetZidTagScoreByAtokenResponse;
import com.aliyun.antirisk20221128.models.GetZidTagScoreByAtokenResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.project.android.model.UserDailyActivityVo;
import com.cema.manage.project.android.service.BizAntiCheatService;
import com.cema.manage.project.manage.antiCheat.domain.AntiCheat;
import com.cema.manage.project.manage.antiCheat.service.IAntiCheatService;
import com.cema.manage.project.manage.antiCheatDetail.domain.AntiCheatDetail;
import com.cema.manage.project.manage.antiCheatDetail.service.IAntiCheatDetailService;
import com.cema.manage.project.manage.users.domain.Users;
import com.cema.manage.project.manage.users.service.IUsersService;
import com.cema.manage.project.util.UmUtils;
import com.cema.manage.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class BizAntiCheatServiceImpl implements BizAntiCheatService {
    @Resource
    private IAntiCheatService antiCheatService;
    @Resource
    private IAntiCheatDetailService antiCheatDetailService;
    @Resource
    private IUsersService usersService;

    @Resource
    private RedisService redisService;

    private static final Logger log = LoggerFactory.getLogger(BizAntiCheatServiceImpl.class);

    @Override
    public void gatherAntiCheat(Users users, UserDailyActivityVo userDailyActivityVo, String ip) throws Exception {

        GetZidTagScoreByAtokenResponse aTokenResponse = UmUtils.getZidTagScoreByAtoken(userDailyActivityVo.getUmToken());
        if (ObjectUtil.isNull(aTokenResponse)) {
            return;
        }
        if (aTokenResponse.getStatusCode() != 200) {
            log.debug(aTokenResponse.getBody().getMsg());
            return;
        }

        EntityWrapper<AntiCheat> entityWrapper = new EntityWrapper<>();
        AntiCheat paraAntiCheat = new AntiCheat();
        paraAntiCheat.setUsId(users.getId());
        entityWrapper.setEntity(paraAntiCheat);
        AntiCheat resultAntiCheat = antiCheatService.selectOne(entityWrapper);
        //构建风控对象
        GetZidTagScoreByAtokenResponseBody.GetZidTagScoreByAtokenResponseBodyData bodyData = aTokenResponse.getBody().getData();
        AntiCheat antiCheat = new AntiCheat();
        antiCheat.setUsId(users.getId());
        antiCheat.setUsNickname(users.getNickname());
        antiCheat.setAHook(Integer.valueOf(bodyData.getAHook()));
        antiCheat.setDebug(Integer.valueOf(bodyData.getDebug()));
        antiCheat.setDoubleOpen(Integer.valueOf(bodyData.getDoubleOpen()));
        antiCheat.setJavaHook(Integer.valueOf(bodyData.getJavaHook()));
        antiCheat.setNativeHook(Integer.valueOf(bodyData.getNativeHook()));
        antiCheat.setRiskLevel(bodyData.getRiskLevel());
        antiCheat.setRiskScore(Integer.valueOf(bodyData.getRiskScore()));
        antiCheat.setRoot(Integer.valueOf(bodyData.getRoot()));
        antiCheat.setSimulator(Integer.valueOf(bodyData.getSimulator()));
        antiCheat.setVpnProxy(Integer.valueOf(bodyData.getVpnProxy()));
        antiCheat.setWifiProxy(Integer.valueOf(bodyData.getWifiProxy()));
        antiCheat.setZid(bodyData.getZid());
        antiCheat.setUpdateTime(new Date());
        if (ObjectUtil.isNull(resultAntiCheat)) {
            //创建新记录
            antiCheatService.insert(antiCheat);
            return;
        }
        //更新记录
        antiCheat.setId(resultAntiCheat.getId());
        antiCheatService.updateById(antiCheat);
        int count = users.getLoginDayNumber()+1;
        users.setLoginDayNumber(count);
        int loginInDayNumber = users.getLoginInDayNumber()+1;
        users.setLoginInDayNumber(loginInDayNumber);
        //自动风控
        //模拟器直接封号
        if (antiCheat.getSimulator() == 1) {
            AntiCheatDetail antiCheatDetail = new AntiCheatDetail();
            antiCheatDetail.setUsId(users.getId());
            antiCheatDetail.setAcdDesc("模拟器");
            antiCheatDetailService.insert(antiCheatDetail);
            //保存用户状态
            Users upUsers = new Users();
            upUsers.setStatus(2);
            upUsers.setId(users.getId());
            usersService.updateById(upUsers);
            //更新缓存
            users.setStatus(2);
        }
        redisService.set(users.getOpenid()+users.getDeviceId(), users);
    }
}
